package leetcode.editor.cn;
//[74]搜索二维矩阵
public class SearchA2dMatrix74{
public static void main(String[] args) {
   Solution solution = new SearchA2dMatrix74().new Solution();
   
}

  //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    //二分查找。 数组 也是 线性有序递增的。
    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length < 1 || matrix[0].length<1) return false;

        int row = matrix.length;
        int col = matrix[0].length;
        //matrix[row][col];
        int len = row * col;
        int left = 0 ;
        int right = len - 1 ;
        while (left <= right){
            int mid = left + (right - left) / 2;
            int rowIndex = mid / col;
            int colIndex = mid % col;
            if (target == matrix[rowIndex][colIndex]){
                return true;
            }else if (target > matrix[rowIndex][colIndex]){
                left = mid + 1;
            }else {
                right = mid - 1;
            }
        }
        return false;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}